package org.example.bytebuf;

import io.netty.buffer.ByteBuf;
import io.netty.buffer.ByteBufAllocator;

import java.nio.charset.StandardCharsets;

/**
 * @author hh
 * @since 2024/10/31
 */
public class TestDirectBuffer {
    public static void main(String[] args) {
        ByteBuf directBuffer = ByteBufAllocator.DEFAULT.directBuffer();
        directBuffer.writeBytes("你好，世界".getBytes(StandardCharsets.UTF_8));
        if (!directBuffer.hasArray()) {
            int length = directBuffer.readableBytes();
            byte[] array = new byte[length];
            // 把数据读取到堆内存array中，再进行Java处理
            directBuffer.getBytes(directBuffer.readerIndex(), array);
            System.out.println(new String(array, StandardCharsets.UTF_8));
        }
        directBuffer.release();
    }
}
